
const Keys = {
    Record: "RenzhengUploadRecord",
    BusLic: "BusinessLicense",
    WorkCer: "WorkCertificate",
    ShopPic: "ShopPicture",
}

export default class UploadRecord {
    constructor(id = "") {
        this.recordId = id;
    }

    get StorageRecord() {
        let record = DZ.storage.get(Keys.Record + this.recordId, true);
        if (record) {
            return record;
        }
        return {};
    }

    set StorageRecord(value) {
        DZ.storage.set(Keys.Record + this.recordId, value);
    }


    static uploadImg(callback, bgimage) {
        // var json = {
        //     "cropscale": 0,
        //     "dialogShowPic": "https://img.dianzhangzhipin.com/blueapp/images/photo/image/20180907/69139487b8830d6303ab6767db819f52.jpeg"
        // }
        // KZ.uploadPic(json)

        KZ.onUploadPhotoComplete && KZ.onUploadPhotoComplete({
            minWidth: 40,
            minHeight: 47, //minWidth上传允许的最小宽度minHeight 上传允许的最大高度
            cut: false,  // cut:false 不裁切, cut:true 裁切
            album: 0, // album:0 拍照和本地图片, album:1只有拍照模式
            showScale: 1,
            dialogShowPic: bgimage || "",
            loading: () => {
                console.log("upload loading");
            },
            success: (reply) => {
                console.log("upload success", reply);
                callback(reply);
            },
            fail: (err) => {
                DZ.form.Prompt("上传失败，请再次尝试", 1)
                console.error("upload fail", err);
            }
        })
    }

    static requestAuthInfo(callback) {
        $.ajax({
            url: API.renzheng.authinfo,
            type: "get",
            dataType: "json"
        }).then(reply => {
            callback(reply);
        }).catch(err => {
            console.error("audit fail", err);
        });
    }

    //营业执照
    getBusLic = () => {
        return this.StorageRecord[Keys.BusLic] || {};
    }
    //工作证明
    getWorkCer = () => {
        return this.StorageRecord[Keys.WorkCer] || {};
    }
    //店铺门面照
    getShopPic = () => {
        return this.StorageRecord[Keys.ShopPic] || {};
    }


    //营业执照
    setBusLic(value = "") {
        let record = this.StorageRecord;
        record[Keys.BusLic] = value;
        this.StorageRecord = record;
    }
    //工作证明
    setWorkCer(value = "") {
        let record = this.StorageRecord;
        record[Keys.WorkCer] = value;
        this.StorageRecord = record;
    }
    //店铺门面照
    setShopPic(value = "") {
        let record = this.StorageRecord;
        record[Keys.ShopPic] = value;
        this.StorageRecord = record;
    }

    clearStorage() {
        DZ.storage.remove(Keys.Record + this.recordId);
    }

    //检查是否上传完成指定的数量
    checkComplete = (num = 0) => {
        let completeNum = 0;
        let record = this.StorageRecord;

        for (const key in record) {
            if (record[key]) {
                completeNum += 1
            }
        }

        return completeNum >= num
    }

    //发起请求，提交审核
    requestSubmitAudit = () => {
        let busLic = this.getBusLic();
        let workCer = this.getWorkCer();
        let shopPic = this.getShopPic();

        $.ajax({
            url: API.renzheng.submitAudit,
            type: "post",
            dataType: "json",
            data: {
                companyName: busLic.name,
                businessLicenceUrl: busLic.img,
                workCertificateUrl: workCer.img,
                shopUrl: shopPic.img
            }
        }).then(reply => {
            if (reply.rescode == 0) {
                this.clearStorage();
                location.replace("success_use.html")
                return;
            }

            DZ.form.Prompt(reply.resmsg, 1)
        }).catch(err => {
            DZ.form.Prompt("操作失败", 1)
        })
    }

    //校验且发起请求
    submitAudit = (num = 0) => {
        if (this.checkComplete(num)) {
            this.requestSubmitAudit();
        } else {
            DZ.form.Prompt("提交成功，请继续上传资料", 1)
            setTimeout(() => {
                location.replace("rz_upload.html")
            }, 1000);
        }
    }
}